home *** CD-ROM | disk | FTP | other *** search
- Path: news.panther.net!nemesis!hammy!not-for-mail
- From: gordon@sneaky.lerctr.org (Gordon Burditt)
- Newsgroups: comp.lang.c
- Subject: Re: Check if a file exists?
- Date: 15 Apr 1996 16:50:38 -0500
- Organization: What organization?
- Message-ID: <4kugbe$qi1@hammy.lonestar.org>
- References: <4kp7pg$upe@news-s01.ny.us.ibm.net> <4ktl40$b76@texas.nwlink.com>
- NNTP-Posting-Host: news.hammy.lonestar.org
-
- >Use rename():
-
- >You could try to rename it, and if that fails it's not there,
- >if it works it exists, and then you can rename it back.
-
- It might fail for a number of other reasons, such as a poor
- choice of destination filename, or lack of write permission in
- the directory.
-
- >Or, you can create a temporary file, try to rename it to the
- >name of the file you're testing, if it fails it exists, and
- >if it works it wasn't there, and then you can rename it back.
-
- Careful. If you create a temporary file, try to rename it to
- the name of the file you're testing, it might SUCCEED if the
- new name exists, and you've just wiped out the file of interest,
- and replaced it with the temporary file. ANSI C leaves
- implementation-defined the issue of whether renaming a file
- to another existing file fails or succeeds. UNIX systems usually
- let it succeed.
-
- >This should be faster than fopen, but you would need to have
- >write access to all the files you're testing.
-
- It isn't at all obvious to me that rename() should be faster than
- fopen(). It especially isn't in the case where you have allowed
- a UNIX directory to accumulate way too many tens of thousands of
- files, and a directory search takes minutes. (fopen would scan
- the directory once, stopping when it found the file, and rename
- would have to scan the whole directory looking for an entry with
- the new name, and it likely does a scan for the old name as well).
-
- In the examples above, you require rename and delete access to
- the files, which need not involve write access. Under UNIX,
- you only require write and search access to the directory,
- and NO access to the files themselves.
-
- The UNIX examples above are not supposed to be an example of the
- way it has to be, but only an example of the way it CAN be as
- permitted by ANSI C.
-
- Gordon L. Burditt
- sneaky.lerctr.org!gordon
-